package test1;

import java.util.Scanner;

public class GameTest {
    static Role[] r =new Role[100];
    static Role r1 = new Role("人机1",100);
    static int count=1;
    public static void main(String[] args) {
        r[0]=r1;
        newrole();
        System.out.println("已创建新角色");
        int res=0;
        while(res==0){
            for (Role role : r) {
                if(role!=null){
                    System.out.print(role.getName() + "   ");
                }
            }
            Scanner sc1 = new Scanner(System.in);
            System.out.println("以上为已存在的角色\n请选择继续创建/与他人对战/退出(1/2/3)");
            String choice;
            choice = sc1.next();
            switch (choice) {
                case "1":
                    newrole();
                    break;
                case "2":
                    fucktogether();
                    break;
                case "3":
                    System.out.println("对战游戏已关闭");
                    res= 1;
                    sc1.close();
                    break;
                default:
                    System.out.println("输入了非法数据,请重新输入");
            }
        }
    }

    public static void fucktogether(){
        Scanner sc = new Scanner(System.in);
        String name1 = null;
        String name2 = null;
        while (true) {
            System.out.println("选择出战的角色和要对战的角色");
            name1 = sc.next();
            name2 = sc.next();
            int RoleFlag=0,flag=0;
            for (Role role : r) {
                if(role!=null) {
                    if (role.getName().equals(name1)) {
                        flag = 1;
                        break;
                    }
                }
            }
            for (Role role2 : r) {
                if(role2!=null) {
                    if (role2.getName().equals(name2)) {
                        RoleFlag = 1;
                        break;
                    }
                }
            }
            if(flag==1&&RoleFlag==1){
                System.out.println("角色确认成功");
                break;
            } else if (flag==0||RoleFlag==0) {
                System.out.println("输入了无效角色,请重新选择角色");
            }
        }
        System.out.println("现在展示战斗双方信息");
        Role role1 = new Role();
        Role role2 = new Role();
        int blood1 = 0, blood2 = 0;
        for (Role role : r) {
            if(role!=null){
                if (role.getName().equals(name1) ) {
                    System.out.println("出战角色:\n名称:"+ role.getName() +"  血量:"+role.getBlood());
                    role1 = role;
                    blood1 = role1.getBlood();
                }
                if(role.getName().equals(name2)) {
                    System.out.println("对战角色:\n名称:"+ role.getName() +"  血量:"+role.getBlood());
                    role2 = role;
                    blood2 = role2.getBlood();
                }
            }
        }
        System.out.println("默认由出战角色率先出招");
        while(true){
            role1.fuck(role2);
            if (role1.getBlood()==0){
                System.out.println("获胜方是"+role2.getName());
                break;
            }
            if (role2.getBlood()==0){
                System.out.println("获胜方是"+role1.getName());
                break;
            }
            System.out.println();
            role2.fuck(role1);
            if (role2.getBlood()==0){
                System.out.println("获胜方是"+role1.getName());
                break;
            }
            if (role1.getBlood()==0){
                System.out.println("获胜方是"+role2.getName());
                break;
            }
            System.out.println();
        }
        role1.setBlood(blood1);
        role2.setBlood(blood2);
    }
    public static void newrole(){
        System.out.println("请创建您的角色(输入名称和血量):");
        System.out.println("注:角色的伤害最大值受最大血量限制,当血量达到210点,将不会造成伤害");
        System.out.println("注:且最大血量高低影响触发\"反弹\"的概率");
        Scanner sc = new Scanner(System.in);
        Role rnew=new Role(sc.next(),sc.nextInt());
        r[count]=rnew;
        count++;
    }
}
